sets = {
	list: [],
	currentSet:"",
	selectSet: function(uid) {
		sets.currentSet = uid;
		var sObj = sets.getSetByUID(uid);
		site.setCurrentSite(sObj.sitename);
		site.refresh("local");
	},
	suggestDefaultSet: function() {
		var suggestion = "";
		var cs = site.getCurrentSite();
		for (var i = 0; i < sets.list.length; i++) {
			if (sets.list[i].sitename == cs) {
				suggestion = sets.list[i].uid;
				return suggestion;
				break;
			}
		}
		return suggestion;
	},
	createNew: function(userUID, setName, siteName) {
		var nUid = generateUID();
		var result = remoteData.interpretServerBoolean(remoteData.requestData(ws.CREATESET, {'uuid':userUID,'uid':nUid, 'setname':setName, 'sitename':siteName}));
		sets.list[sets.list.length] = sets.createSetObject(nUid, setName, siteName);
		sets.serializeAndWriteSetsToFileSystem();
		return sets.list[sets.list.length-1];
	},
	createSetObject: function(uid, setname, sitename) {
		return {'uid':uid,'name':setname,'sitename':sitename};
	},
	serializeAndWriteSetsToFileSystem: function() {
		var setText = "";
		for (var i = 0; i < sets.list.length;i++) {
			if (i > 0)
				setText += "----DUSPLIT----";
			setText += sets.list[i].uid+"**ITEMSPLIT**";
			setText += sets.list[i].name+"**ITEMSPLIT**";
			setText += sets.list[i].sitename+"**ITEMSPLIT**";
		}
		// delete it
		if (DWfile.exists(sets.getSetsFileLocation())) {
			DWfile.remove(sets.getSetsFileLocation());
		}
		
		// now write it fresh
		DWfile.write(sets.getSetsFileLocation(), setText);
	},
	deserializeSetsFromFileSystem: function() {
		if (DWfile.exists(sets.getSetsFileLocation())) {
			var rawFileData = DWfile.read(sets.getSetsFileLocation());
			var itemList = rawFileData.split("----DUSPLIT----");
			for (var i = 0; i < itemList.length; i++) {
				var iObj = itemList[i].split("**ITEMSPLIT**");
				sets.list[sets.list.length] = sets.createSetObject(iObj[0],iObj[1],iObj[2]);
			}
		}
	},
	getSetsFileLocation: function() {
		return dw.getConfigurationPath() + "/Shared/du/config/du_sets.dat"; 
	},
	deleteByUID: function(uid, callserver) {
		if (!callserver)
			callserver = true;
		var newList = [];
		for (var i = 0; i < sets.list.length; i++) {
			if (sets.list[i].uid == uid) {
			} else {
				newList[newList.length] = sets.list[i];
			}
		}
		sets.list = newList;
		if (callserver == true)
			var result = remoteData.interpretServerBoolean(remoteData.requestData(ws.DELETESET, {'uid':uid}));
		sets.serializeAndWriteSetsToFileSystem();
	},
	getSetByUID: function(uid) {
		var res = sets.list[0];
		for (var i = 0; i < sets.list.length; i++) {
			if (sets.list[i].uid == uid) {
				res = sets.list[i];
			}
		}
		return res;
	},
	init: function() {
		
		// First get the sets from the file system if possible
		sets.deserializeSetsFromFileSystem();
		
	}
}